package testcase.login;

import basic.BasicCase;
import io.appium.java_client.android.AndroidKeyCode;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

//判断app账号是否登录，如果未登录，则进行登录，如果已经登录，就继续下一步操作
public class LoginCase extends BasicCase {

    @Test
    public void testName() throws InterruptedException {
        Thread.sleep(6000);
        isNeedLogin();

        }

//      //  driver.findElementById("com.oppo.ohome:id/iv_add").click();
//        //获取屏幕宽和高
//        int width=driver.manage().window().getSize().width;
//        int height=driver.manage().window().getSize().height;
//        driver.swipe(width/2,height*1/4,width/2,height*3/4,2000);
//        driver.findElementByXPath(".//*[@text='']");
    public static  void isNeedLogin() throws InterruptedException {
        //判断当前是否登录
        try{
            driver.findElement(By.id("com.oppo.ohome:id/tv_title_sub"));
            System.out.println("账号已经登录");
        }catch(Exception e){
            System.out.println("账号未登录");
            driver.findElement(By.id("com.oppo.ohome:id/tv_title")).click();
//          finder.waitforWebElement("com.oppo.usercenter:id/oppo_preference");
            //找到用户名输入框

            WebElement username=driver.findElement(By.id("com.oppo.usercenter:id/bottom_line"));
            if(driver.findElement(By.id("com.oppo.usercenter:id/bottom_line")).getText()!=null){
                System.out.println("已经存在用户名");
            }else{
                username.click();
                username.clear();
                username.sendKeys("13714526025");
            }

            //找到密码输入框
            WebElement pwd=driver.findElement(By.id("com.oppo.usercenter:id/edit_input_content"));
            pwd.click();
            pwd.clear();
            pwd.sendKeys("youyou961115");
            //点击登录按钮
            driver.findElement(By.id("com.oppo.usercenter:id/btn_login")).click();
            Thread.sleep(3000);
          try{
              driver.findElement(By.name("取消")).click();
          }catch(Exception e1){
              e1.printStackTrace();
          }
            //返回
            driver.pressKeyCode(AndroidKeyCode.BACK);
            //判断是否出现设置按钮
            Assert.assertTrue(driver.findElement(By.id("com.oppo.ohome:id/iv_setting")).isDisplayed());
        }


    }

    }

